package com.org.csu.service.Impl;

import com.org.csu.dao.RecordRepository;
import com.org.csu.dao.UserRecordRepository;
import com.org.csu.dto.KeywordDTO;
import com.org.csu.entity.CompKey;
import com.org.csu.entity.KeyRecord;
import com.org.csu.entity.UserRecord;
import com.org.csu.entity.Word;
import com.org.csu.service.KeywordRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author YigongLily
 * @version V1.0
 * Copyright (c) 2024, t8669@qq.com All Rights Reserved.
 * @ProjectName:e-commerce-back
 * @Title: KeywordRecordServiceImpl
 * @Package com.org.csu.service.Impl
 * @Description: kwrsImpl
 * @date 2024/12/15 12:07
 */
@Service
public class KeywordRecordServiceImpl implements KeywordRecordService {
    @Autowired
    private UserRecordRepository userRecordRepository;
    @Autowired
    private RecordRepository recordRepository;
    @Override
    public void insertKeywordRecord(String keyword, List<KeywordDTO> mid, List<CompKey> comp, List<Word> trend, Integer rate, Integer userId) {
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");
        String formattedDate = currentDate.format(formatter);

        // 检查是否已经存在相同日期和关键词的记录
        KeyRecord existingRecord = recordRepository.findRecordByDateAndKeyword(formattedDate, keyword);
        if (existingRecord != null) {
            // 如果记录已存在，你可以选择更新记录或者什么都不做
            // 例如，这里我们选择更新记录中的某些字段
            existingRecord.setTrend(trendConvert(trend));
            existingRecord.setMidword(midConvert(mid));
            existingRecord.setCompword(compConvert(comp));
            recordRepository.save(existingRecord);
        } else {
            // 如果记录不存在，创建新记录
            KeyRecord keyRecord = new KeyRecord();
            keyRecord.setKeyword(keyword);
            keyRecord.setDate(formattedDate);
            keyRecord.setRate(rate);
            keyRecord.setTrend(trendConvert(trend));
            keyRecord.setMidword(midConvert(mid));
            keyRecord.setCompword(compConvert(comp));
            recordRepository.save(keyRecord);
        }

        // 无论记录是新创建还是更新，都需要保存用户记录
        UserRecord userRecord = new UserRecord();
        userRecord.setUserId(userId);
        // 如果记录是新创建的，recordRepository.save(keyRecord)会返回新创建的记录的ID
        // 如果记录是更新的，existingRecord.getRecordId()会返回更新的记录的ID
        userRecord.setRecordId(recordRepository.findRecordByDateAndKeyword(formattedDate, keyword).getRecordId());
        userRecord.setKeyword(keyword);
        userRecord.setDate(formattedDate);
        userRecordRepository.save(userRecord);
    }

    public String midConvert(List<KeywordDTO> mid){
        StringBuilder result = new StringBuilder();
        for (KeywordDTO keywordDTO : mid) {
            if (result.length() > 0) {
                result.append(";");
            }
            result.append(keywordDTO.getWord() + "," + keywordDTO.getHot());
        }

        String finalResult = result.toString();
        return finalResult;
    }

    public String compConvert(List<CompKey> comp){
        StringBuilder result = new StringBuilder();
        for (CompKey compKey : comp) {
            if (result.length() > 0) {
                result.append(";");
            }
            result.append(compKey.getWord() + "," + compKey.getHot() + "," + compKey.getComp());
        }

        String finalResult = result.toString();
        return finalResult;
    }

    public String trendConvert(List<Word> trend){
        StringBuilder result = new StringBuilder();
        for (Word word : trend) {
            if (result.length() > 0) {
                result.append(";");
            }
            result.append(word.getWord() + "," + word.getCount());
        }

        String finalResult = result.toString();
        return finalResult;
    }


    //暂时不做
    @Override
    public Object getKeywordRecord(String userId) {
        return null;
    }
}